import { _decorator, Component, director, Label, NodeEventType, Sprite, SpriteFrame } from 'cc';
import task from '../sys/TaskSys';
import { aa } from '../frame/FrameCore';
import { aaEvent, Key, RewardIcon, RewardType, TaskConfig, TaskType } from '../frame/common/Enums';
import reward from '../sys/RewardSys';
const { ccclass, property } = _decorator;
const TaskDes = [
    "完成跑酷游戏",
    "完成答题游戏",
    "好感度提升到",
    "跑酷获得金币",
    "答题游戏挑战",
    "故事模式通关"
]
const week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"];
@ccclass('TaskView')
export class TaskView extends Component {
    @property(Sprite) taskBg: Sprite;
    @property(Sprite) taskIcon: Sprite;
    @property(Label) taskDes: Label;
    @property(Label) taskGoal: Label;
    @property(SpriteFrame) rewardIcons: SpriteFrame[] = [];
    private _config: TaskConfig = null;
    private _showed = false;


    onEnable() {
        this.node.on(NodeEventType.TOUCH_END, this.getReward, this);
        director.on(aaEvent.updateLocal, this.updateTask, this);
        director.on(aaEvent.updateTaskAmount, this.updateTaskAmount, this);
        this.updateTask();

    }
    onDisable(): void {
        director.off(aaEvent.updateTaskAmount, this.updateTaskAmount, this);
        director.off(aaEvent.updateLocal, this.updateTask, this);

    }

    getReward() {
        if (this._config.goal == aa.data.get(Key.CurrentTaskAmount)) {
            const rewardType = this._config.rewardType;
            const amount = this._config.rewardAmount;
            switch (rewardType) {
                case RewardType.HEART:
                    reward.showCashView(amount)
                    // reward.addDiamond(amount)
                    break;
                case RewardType.COIN:
                    reward.showCashView(amount)
                    break;
            }
            task.nextTask();
            this.updateTask();
        }else{
            switch (this._config.type) {
                case TaskType.HEART:
                    director.emit(aaEvent.functionNotice, 0);
                    break;
                case TaskType.STORY:
                    director.emit(aaEvent.functionNotice, 1);
                    break;
                case TaskType.QUIZ:
                case TaskType.QUIZLEVEL:
                    director.emit(aaEvent.functionNotice, 2);
                    break;
                case TaskType.COIN:
                case TaskType.GAME:
                    director.emit(aaEvent.functionNotice, 3);
                    break;
                default:
                    break;
            }
        }

    }

    updateTaskAmount(type: TaskType, amount = 1) {
        if (type != this._config.type) return;
        let current
        if (type == TaskType.HEART) {
            current = Math.floor(aa.data.get(Key.Hearts) * 10) / 10
            // current = level.currentLevel;
        } else {
            current = aa.data.get(Key.CurrentTaskAmount);
            current += amount;
        }
        if (current >= this._config.goal) {
            current = this._config.goal;
            aa.data.set(Key.CurrentTaskAmount, current);
            this.taskBg.customMaterial = aa.res.getMaterial("blink");
        } else {
            aa.data.set(Key.CurrentTaskAmount, current);
        }
        this.updateGoal(current);
    }

    updateTask() {
        this._config = task.getTask();
        this.taskIcon.spriteFrame = this.rewardIcons[this._config.rewardType];
        const des = TaskDes[this._config.type];
        let current;

        switch (this._config.type) {
            case TaskType.HEART:
                current = Math.floor(aa.data.get(Key.Hearts) * 10) / 10;
                current = Math.min(current, this._config.goal);
                aa.data.set(Key.CurrentTaskAmount, current);
                if(!this._showed) director.emit(aaEvent.functionNotice, 0);
                break;
            case TaskType.STORY:
                current = Math.floor(aa.data.get(Key.CurrentLevel));
                current = Math.min(current, this._config.goal);
                aa.data.set(Key.CurrentTaskAmount, current);
                if(!this._showed) director.emit(aaEvent.functionNotice, 1);
                break;
            case TaskType.QUIZ:
            case TaskType.QUIZLEVEL:
                // Assuming the same functionNotice should be emitted for both QUIZ and QUIZLEVEL
                if(!this._showed) director.emit(aaEvent.functionNotice, 2);
                current = aa.data.get(Key.CurrentTaskAmount);
                break;
            case TaskType.COIN:
            case TaskType.GAME:
                // Assuming the same functionNotice should be emitted for both COIN and GAME
                if(!this._showed) director.emit(aaEvent.functionNotice, 3);
                current = aa.data.get(Key.CurrentTaskAmount);
                break;
            default:
                current = aa.data.get(Key.CurrentTaskAmount);
                break;
        }

        if (current == this._config.goal) {
            this.taskBg.customMaterial = aa.res.getMaterial("blink");
        } else {
            this.taskBg.customMaterial = null;
        }
        this.updateGoal(current);
        this.taskDes.string = des;
        this._showed = true;
    }

    updateGoal(num) {
        switch (this._config.type) {
            case TaskType.HEART:
                this.taskGoal.string = `${num}/${this._config.goal}好感度`
                break;
            case TaskType.STORY:
                const index = Math.floor(this._config.goal / 4);
                const task = 1+this._config.goal % 4;
                this.taskGoal.string = `${week[index]}第${task}关`
                break;
            case TaskType.QUIZLEVEL:
                this.taskGoal.string = `难度${this._config.goal}`
                break;
            case TaskType.COIN:
                this.taskGoal.string = `${num}/${this._config.goal}金币`
                break;
            default:
                this.taskGoal.string = `${num}/${this._config.goal}`
                break;
        }
    }


}

